


var $submit_xm_update = $('#submit_xm_update'),
$submit_xm_add = $('#submit_xm_add'),
$submit_az = $('#submit_az')


$(function () {

    bindDate()
    //提交修改操作
    $submit_xm_update.click(function () {
        treeUpdate()
    })
    //提交添加操作
    $submit_xm_add.click(function () {
        treeAdd()
    })
    //提交授权操作
    $submit_az.click(function () {
        pastAuthorize()
    })
    /**
     * 判断项目负责人id是否可用
     * @type {b.fn.init|jQuery|HTMLElement}
     */
    var o_text = $('#xmOwnerid_text')
    var sub_xm = $('#submit_xm')
    var xmOwnerid = $("input[name='xmOwnerid']")

    xmOwnerid.blur(function (){
        $.ajax({
            type: "get",
            url: "verificationOwnerid",
            data: "xmOwnerid=" + xmOwnerid.val(),
            success: function (data) {
                if (data.result){
                    o_text.text('该项目负责人id可以添加').css('color','green')
                    sub_xm.removeAttr('disabled')
                }else{
                    o_text.text('该项目负责人id已经存在').css('color','red')
                    sub_xm.attr('disabled','disabled')
                }
            }
        })
    })

})

/**
 * 已授权查询
 */
function getFind() {
    var $thead_data = $('#thead_data')
    var $tbody_data = $('#tbody_data')
    $thead_data.empty()
    $tbody_data.empty()
    $thead_data.append("<tr>" +
        "<th>ID</th>" +
        "<th>部门代码</th>" +
        "<th>项目代码</th>" +
        "<th>被授权人ID</th>" +
        "<th>操作时间</th>" +
        "</tr>")
    $.ajax({
        url: 'getAuthorizelistall',
        type: 'get',
        success: function (data) {
            for (var x in data) {
                $tbody_data.append("<tr>" +
                    "<td>"+data[x].sqId+"</td>" +
                    "<td>"+data[x].sqBmdm+"</td>" +
                    "<td>"+data[x].sqXmdm+"</td>" +
                    "<td>"+data[x].sqOwnerid+"</td>" +
                    "<td>"+data[x].sqTime+"</td>" +
                    "</tr>")
            }
        }
    })//ajax请求
}

/**
 * 未授权查询
 */
function getUn() {
    var $thead_data = $('#thead_data')
    var $tbody_data = $('#tbody_data')
    $thead_data.empty()
    $tbody_data.empty()
    $thead_data.append("<tr>" +
        "<th>部门代码</th>" +
        "<th>项目代码</th>" +
        "<th>项目级次</th>" +
        "<th>项目名称</th>" +
        "<th>项目负责人ID</th>" +
        "<th>项目明细</th>" +
        "<th>项目有效金额</th>" +
        "<th>创建时间</th>" +
        "<th>修改时间</th>" +
        "</tr>")
    $.ajax({
        url: 'getShow',
        type: 'get',
        success: function (data) {
        for (var x in data) {
            var xmName =  data[x].xmName
            var bmName =  data[x].bmName
            var xmOwnerid =  data[x].xmOwnerid
            if (xmName == null){
                xmName = '空'
            }
            if (bmName == null){
                bmName = '空'
            }
            if (xmOwnerid == null) {
                xmOwnerid = '空'
            }
            $tbody_data.append("<tr>" +
                "<td>"+data[x].bmDm+"</td>" +
                "<td>"+data[x].xmDm+"</td>" +
                "<td>"+data[x].bxLevel+"</td>" +
                "<td>"+xmName+"</td>" +
                "<td>"+xmOwnerid+"</td>" +
                "<td>"+data[x].xmDetailed+"</td>" +
                "<td>"+data[x].xmJeyx+"</td>" +
                "<td>"+data[x].xmTime+"</td>" +
                "<td>"+data[x].xmOldtime+"</td>" +
                "</tr>")
        }
    }
})//ajax请求
}

/**
 * 授权提交
 */
function pastAuthorize() {
    var ownerid = $("input[name='xmOwnerid']").val()
    var bmDm = $("input[name='xmDm']").val()
    var xmDm = bmDm.substring(0,bmDm.length-2)
    var $error_xm = $('#error_xm')
    // alert(bmDm+","+xmDm+","+ownerid)
    $.ajax({
        type: "post",
        url: "pastAuthorize",
        data: {
            "sqBmdm":bmDm,
            "sqXmdm":xmDm,
            "sqOwnerid":ownerid
        },
        success: function (data) {
            if (data.result){
                $error_xm.text('项目授权成功').css('color','green')
                close()
            }else{
                $error_xm.text('项目授权失败，后台有日志错误').css('color','red')
            }
        }
    })
}
function bindDate() {
    /**
     * 加载添加面板并且提交添加数据
     * @type {Array}
     */
    var selectedValues = [];
    var $selectpicker = $('.selectpicker')//下拉框属性
    $("slpk:selected").each(function(){
        selectedValues.push($(this).val());
    });
    $selectpicker.selectpicker({
        noneSelectedText : '请选择'
    });
    $(window).on('load', function() {
        $selectpicker.selectpicker('val', '');
        $selectpicker.selectpicker('refresh');
    });
    //下拉数据加载
    $.ajax({
        type : 'get',
        url : "getTreeType",
        dataType : 'json',
        success : function(datas) {//返回list数据并循环获取
            var select = $("#slpk");
            for (var i = 0; i < datas.length; i++) {
                var dmDmlength = datas[i].bmDm.length
                if (dmDmlength == 3){
                    select.append("<option value='"+datas[i].bmDm+"'>"
                        + datas[i].bmName + "</option>");
                }
                if (dmDmlength > 3){
                    select.append("<option value='"+datas[i].bmDm+"'>"
                        + datas[i].xmName + "</option>");
                }
            }
            $selectpicker.selectpicker('val', '');
            $selectpicker.selectpicker('refresh');
        }

    })
}

/**
 * 根据对应的代码创建当前节点的代码
 * @param value
 */
function getDm(value) {
    $.ajax({
        type: "get",
        url: "getChildren",
        data:"xmDm="+value,
        success: function (data) {
            var index = data.length+1
            var newDm = value + "0" + index
          // alert(newDm)
            $("input[name='xmDm']").val(newDm).attr('disabled','disabled')
        }
    })
}

/**
 * 添加操作
 * @param value
 */
function treeAdd() {
    var value = getAddemodal()
    //alert(value)
    var $error_xm = $('#error_xm')
    var xmNames = $("input[name='xmNames']").val()
    var bmNames = $("input[name='bmNames']").val()
    var xmDm = $("input[name='xmDm']").val()
    var xmOwnerid = $("input[name='xmOwnerid']").val()
    var xmJea = $("input[name='xmJea']").val()
    var xmJezb = $("input[name='xmJezb']").val()
    var xmJejc = $("input[name='xmJejc']").val()
    var xmTime = $("input[name='xmTime']").val()
    $('#add_text').show()
    $.ajax({
        type: "get",
        url: "addProject",
            data: {
            "xmName":xmNames,
            "bmName":bmNames,
            "xmDm":value,
            "bmDm":xmDm,
            "xmOwnerid":xmOwnerid,
            "xmJea":xmJea,
            "xmJezb":xmJezb,
            "xmJejc":xmJejc,
            "xmTime":xmTime,
        },
            success: function (data) {
            if (data.result){
                $error_xm.text('项目添加成功').css('color','green')
                $submit_xm_add.removeAttr('disabled')
                close()
            }else{
                $error_xm.text('项目添加失败，后台有日志错误').css('color','red')
                $submit_xm_add.attr('disabled','disabled')
            }
        }
    })
}
/**
 * 初始化添加模态框
 */
var value
function getAddemodal() {
   // close()
    $('#myModalLabel').text('添加节点')
    $('.select_bm').show()
    $('.oldTime_text').hide()
    $('.xmId_text').hide()
    $('.xmTime_text').show()
    $('.xmOwnerid_text').show()
    $('.xmDm_text').show()
    $('#add_text').hide()
    $('.az').hide()
    $submit_xm_update.hide()
    $submit_xm_add.show()
    //点击选中
    $('.selectpicker').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
        //对选中的当前节点赋值对应的子节点
        $('#add_text').show()
        value = $(this).val()
        getDm(value)

    })
    return value
}
/**
 * 初始化修改模态框
 */
function getUpdatemodal(bmDm) {
    $submit_xm_update.show()
    $submit_xm_add.hide()
    $('.xmId_text').hide()
    $('#myModalLabel').text('修改节点')
    $("input[name='xmDm']").removeAttr('disabled')
    $('.select_bm').hide()
    $('.xmDm_text').hide()
    $('.xmOwnerid_text').hide()
    $('#add_text').show()
    $('.oldTime_text').show()
    $('.xmTime_text').hide()
    $('.az').show()
    $.ajax({
        url:"getProjectbm",
        type: 'get',
        data:'bmDm=' + bmDm,
        success: function (data) {
            $("input[name='xmId']").val(data.xmId)
            $("input[name='xmNames']").val(data.xmName)
            $("input[name='xmDm']").val(data.bmDm)
            $("input[name='xmOwnerid']").val(data.xmOwnerid)
            $("input[name='xmJea']").val(data.xmJea)
            $("input[name='xmJezb']").val(data.xmJezb)
            $("input[name='xmJejc']").val(data.xmJejc)
            $("input[name='xmOldtime']").val(data.xmOldtime)
        }
    })
}

/**
 * 项目修改
 */
function treeUpdate() {
    var $error_xm = $('#error_xm')
    $.ajax({
        type: "get",
        url: "updateProject",
        data: {
            "xmId":$("input[name='xmId']").val(),
            "xmName":$("input[name='xmNames']").val(),
            "xmOwnerid":$("input[name='xmOwnerid']").val(),
            "xmJea":$("input[name='xmJea']").val(),
            "xmJezb":$("input[name='xmJezb']").val(),
            "xmJejc":$("input[name='xmJejc']").val(),
            "xmOldtime":$("input[name='xmOldtime']").val(),
        },
        success: function (data) {
            if (data.result){
                $error_xm.text('项目修改成功').css('color','green')
            }else{
                $error_xm.text('项目修改失败，后台有日志错误').css('color','red')
            }
        }
    })
}
